<template>
    <div class="shop-cart">
<button @click="back">返回</button>
        <div class="lists">
            <div class="header">
                <ul class="clearfix row">
                    <li>
                        <!-- <input type="checkbox" class="select-all" v-model="allchecked" @change="allcheck()"> -->
                        <span :class="[allchecked?'ckb_s':'ckb']" @click="allcheck()"></span>全选
                    </li>
                    <li>商品信息</li>
                    <li>单价</li>
                    <li>尺码</li>
                    <li>数量</li>
                    <li>金额</li>
                </ul>
            </div>
            <hr>
            <!-- 实验需要操作的节点 -->
            <div id="shoplist" class="shoplist">
                <!-- 店铺内容及样式参考 -->
                <div class="shop" v-for="shopcart in shopcarts" :key="shopcart.shopId">
                    <!--  -->
                    <div class="products">
                        <div v-for="product in shopcart.products" :key="product" :class="['item',product.ischecked?'even':'']">
                            <div class="select-logo">
                                <!-- <input type="checkbox" v-model="product.ischecked" @change="productcheck(shopcart,product)"> -->
                                <span :class="[product.ischecked?'ckb_s':'ckb']" @click="productcheck(shopcart,product)"></span>
                                <img :src="product.goodsimg" alt="产品logo" @click="toabout(product)" style="cursor:pointer">
                            </div>
                            <div class="product-detail">
                                <div class="left" @click="toabout(product)">
                                    <a class="product-name">
                                        {{product.goodsname}}
                                    </a>
                                </div>
                                <div class="right" v-html="product.spec">
                                    
                                </div>
                            </div>
                            <div class="price">
                                <p class="market-price">￥{{product.marketPrice}}</p>
                                <p class="real-price">￥{{product.goodsprice}}</p>
                            </div>
                            <div>
                                <select :v-model="value" class="col-7">
                                    <option value="s">s</option>
                                    <option value="m">m</option>
                                    <option value="l">l</option>
                                    <option value="xl">xl</option>
                                </select>
                            </div>
                            <div class="count">
                                <span :class="['sub',product.count==1?'sub_s':'sub']" @click="decrement(shopcart,product)">-</span>
                                <span class="input"><input type="text" v-model="product.count"></span>
                                <span class="add" @click="increment(shopcart,product)">+</span>
                               
                            </div>
                            <div class="amount">￥{{product.goodsprice*product.count}}</div>
                            <div class="delete">
                                <button @click="remove(shopcart,product)">删除</button>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
    <div class="operate row justify-content-center">
        <div class="left col-4">
            <button @click="removeAll()" v-show="allchecked">全部删除</button>
        </div>

        <div class="right col-4">
            <div>已选择商品<span class="selected-count">{{countnumber}}</span>件</div>
            <div class="amount">合计：<span class="sum" >￥{{sumprice}}</span></div>
            <button class="settle" @click="pay">结算</button>
        </div>
    </div>
    
</template>
<script>
import axios from 'axios'
export default {
    data(){
        return{
            shopcarts:[],
            shopgoods:{},
            allchecked:false,
            indexsum:0
        }
    },
    created(){
        if(!this.$store.state.userInfo.username){
            this.$router.push({
                path: '/'
            })
        }
        axios.get(`http://127.0.0.1:4523/m1/1263105-0-default/shopcarts`).then(
            (res)=>{
            this.shopcarts=res.data.shopcarts;
            if(this.$route.params.id){
                this.shopgoods=JSON.parse(this.$route.params.id)
                console.log(this.shopgoods)
                this.shopcarts.forEach(shopcart=>{
                    shopcart.products.forEach(product=>{
                        if(product.goodsname===this.shopgoods.goodsname){
                            this.shopgoods.count+=product.count
                            shopcart.products.splice(shopcart.products.indexOf(product),1)
                        }
                    })
                    shopcart.products.unshift(this.shopgoods)
                    // console.log(shopcart)
                })
        }
            }
        ).catch((error)=>{
            console.log(error)
        })
        
        
    },
    // 形成商品连接
   
    // 计算属性方式
    computed:{
        // 结算--价格
        sumprice(){
            let sum=0;
            this.shopcarts.forEach(shopcart => {
                shopcart.products.forEach((product)=>{
                    if(product.ischecked){
                        sum+=(product.goodsprice*product.count);
                    }
                })
                
            });
            return sum;
        },
        // 商品数量
        countnumber(){
            let countnumber=0;
            this.shopcarts.forEach(shopcart => {
                    shopcart.products.forEach((product)=>{
                        if(product.ischecked){
                            countnumber+=product.count;
                        }
                    })
            });
            return countnumber;
        }
        
    },

    methods:{
        toabout(x){
            this.$router.push({
                name:"about",
                params:{
                    id:JSON.stringify(x)
                }
            })
            },
        pay(){
            const sum=[];
            let i=0;
            this.shopcarts.forEach(shopcart => {
                shopcart.products.forEach((product)=>{
                    if(product.ischecked){
                        sum.push(product);
                        // console.log(sum)
                        i++;
                    }
                })
                
            });
            if(i){
                alert('购买成功！')
                this.$router.push({
                    name:"order",
                    params:{
                        id:JSON.stringify(sum)
                    }
                })
            }else{
                alert('请选择商品进行购买！')
            }
            
        },
        active(item,index,titles){
            titles.forEach((t)=>{
                t.mclass='title'
            });
            item.mclass="title active";
            this.offsetWidth=(index*123)+'px'
        },
        back(){
            this.$router.go(-1)
        },
        // 商品数量
        decrement(shopcart,product){
            if((product.count-1)>0){
                product.count-=1
            }
        },
        increment(shopcart,product){
            product.count+=1
        },
        
        // 删除商品
        // 单个商品删除
        remove(shopcart,product){
            shopcart.products.splice(shopcart.products.indexOf(product),1);
            if(shopcart.products.length<=0){
                this.shopcarts.splice(this.shopcarts.indexOf(shopcart),1)
            }
            
        },
        // 多个商品删除
        removeAll(){
            if(this.allchecked){
                this.shopcarts.splice(this.shopcarts);
                this.allchecked=false;
            }
        },
                

        // 全选
        allcheck(){
            this.allchecked=!this.allchecked;
            if(this.allchecked){
                this.shopcarts.forEach((shopcart)=>{
                    shopcart.ischecked=true
                    shopcart.products.forEach((product)=>{
                        product.ischecked=true
                    })
                })
            }else{
                this.shopcarts.forEach((shopcart)=>{
                    shopcart.ischecked=false
                    shopcart.products.forEach((product)=>{
                        product.ischecked=false
                    })
                })
            }
        },

        // 店铺选择
        shopcheck(shopcart){
            shopcart.ischecked=!shopcart.ischecked;
            if(shopcart.ischecked){
                shopcart.products.forEach((product)=>{
                    product.ischecked=true
                })
            }else{
                shopcart.products.forEach((product)=>{
                    product.ischecked=false
                    this.allchecked=false

                })
            }
            const num2=this.shopcarts.filter((shopcart)=>{
                return shopcart.ischecked
                }).length
            // console.log(num2);  //查看数据是否有问题
            // console.log(shopcarts.length);

            if(num2===this.shopcarts.length){
                this.allchecked=true
            }else{
                this.allchecked=false
            }
        },

        // 商品选择
        productcheck(shopcart,product){
            product.ischecked=!product.ischecked;
            const num=shopcart.products.filter((product)=>{
                return product.ischecked
                }).length
            
            if(num===shopcart.products.length){
                shopcart.ischecked=true
            }else{
                shopcart.ischecked=false
                this.allchecked=false
            }
            const num2=this.shopcarts.filter((shopcart)=>{
                return shopcart.ischecked
                }).length
            // console.log(num2);
            // console.log(shopcarts.length);

            if(num2===this.shopcarts.length){
                this.allchecked=true
            }else{
                this.allchecked=false
            }

        }
        
    }

}
</script>
<style scoped>
 * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Tahoma, Arial, Helvetica, sans-serif;
    font-size: 12px;
    line-height: 1.2;
}
.shop-cart .even{
    background-color: rgb(112, 204, 244,0.2);
}
ul {
    list-style: none;
}

.clearfix::after {
    /* 设置添加的内容，空为了让用户看不见。 */
    content: '';
    /* 控制添加的伪元素是块级元素 */
    display: block;
    /* 清除浮动 */
    clear: both;
    /* 为了让用户看不见。 */
    visibility: hidden;
    height: 0;
    font-size: 0;
}

.clearfix {
    /* css hack  星号表示IE7以下的浏览器，主要为了兼容IE6 */
    *zoom: 1;
}

.shop-cart {
    width: 990px;
    margin: 10px auto;
    /* border: 1px solid red; */
    user-select: none;
}

.shop-cart button{
   width: 80px;
    height: 40px;
    background-color: white;
    border: 2px solid #93d2f3;
}
.shop-cart button:hover{
    background-color: #93d2f3;
    color: white;
}
.tabs {
    height: 35px;
}

.tabs .border {
    border-bottom: 2px solid #e6e6e6;
    position: relative;
}

.tabs .border .line {
    display: inline-block;
    width: 123px;
    height: 2px;
    background: #ff4400;
    position: absolute;
    bottom: -2px;
    left: 0;
    transition: left .5s ease;
}

.tabs .border .title {
    display: inline-block;
    font-size: 16px;
    font-weight: bold;
    padding: 5px 20px 10px 20px;
    cursor: pointer;
}

.tabs .border .title.active {
    color: #ff4400;
}

.tabs .border .title span {
    font-size: 14px;
    color: #ff4400;
}
.operate button{
    width: 100px;
    height: 30px;
    border-radius: 5px;
    color: white;
    border: none;
    background-color: #fa713f;
}
.tabs .border .title:not(.last)::after {
    content: "";
    display: inline-block;
    ;
    width: 1px;
    height: 12px;
    transform: scale(.5, 1);
    background: #e6e6e6;
    position: relative;
    left: 20px;
}

.lists .header ul {
    height: 50px;
    line-height: 50px;
}

.lists .header ul li {
    float: left;
    color: #3c3c3c;
}

.lists .header ul li:not(.product-info) {
    width: 120px;
}
.lists .header ul li:nth-child(2){
    width: 370px;
}
.lists .header ul li:nth-child(3){
    width: 105px;
}
.lists .header ul li:nth-child(4){
    width: 100px;
}
.lists .header ul li:nth-child(5){
    width: 90px;
}

.lists .header ul li.product-info {
    width: calc(990px - (125px * 5));
}

.lists .shop .shop-info {
    height: 50px;
    line-height: 50px;
}

.even{
    background-color: bisque;
}

.ckb{
    width: 1.1rem;/* rem相对浏览器字体大小 */
    height: 1.1rem;
    border-radius: 50%;
    border: 1px solid #B8B4B4;
    display: inline-block;/* 占行，使可以设置大小 */
    cursor: pointer;
    vertical-align: text-bottom;/* 对齐文字底部 */
}
.ckb_s{
    width: 1.1rem;
    height: 1.1rem;
    border-radius: 50%;
    display: inline-block;
    cursor: pointer;
    background: url(./../assets/gouico.png) no-repeat 0 0;/* no-repeat使图片不重复 */
    background-size: 1.1rem;
    border: 0;
    vertical-align: text-bottom;
}

.lists .shop .shop-info .shop-tip::before {
    content: '';
    display: inline-block;
    width: 16px;
    height: 18px;
    background-repeat: no-repeat;
    background-position: -20px -125px;
    position: relative;
    top: 3px;
    margin: 0 5px;
}

.lists .shop .shop-info .shop-name {
    color: #3c3c3c;
    text-decoration: none;
}

.lists .shop .shop-info .shop-name:hover {
    text-decoration: underline;
    color: #ff4400;
}

.lists .shop .shop-info .wangwang {
    display: inline-block;
    width: 20px;
    height: 20px;
    text-indent: -10000px;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: contain;
    position: relative;
    left: 10px;
    top: 4px;
}

.lists .shop .products {
    border: 1px solid #ccc;
}

.lists .shop .products .item {
    display: flex;
    padding: 20px 10px;
    border-bottom: 1px solid #eee;
}

.lists .shop .products .item .select-logo {
    display: flex;
}

.lists .shop .products .item .select-logo img {
    width: 80px;
    height: 80px;
    margin-left: 10px;
}

.lists .shop .products .item .product-detail {
    width: calc(990px - (125px * 5));
    display: flex;
}

.lists .shop .products .item .product-detail .left {
    width: calc(990px - (125px * 5) - 190px);
    position: relative;
}

.lists .shop .products .item .product-detail .left .product-name {
    text-decoration: none;
    color: #3c3c3c;
    /* https://www.daqianduan.com/6179.html */
    /* 多行加省略号 */
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
}

.lists .shop .products .item .product-detail .left .product-name:hover {
    text-decoration: underline;
    color: #ff4400;
}

.lists .shop .products .item .product-detail .left .logos {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
}

.lists .shop .products .item .product-detail .left .logos a {
    text-decoration: none;
    overflow: hidden;
    text-indent: -10000px;
    display: inline-block;
    width: 16px;
    height: 16px;
}

.lists .shop .products .item .product-detail .right {
    width: 190px;
    color: #9c9c9c;
    padding: 0 20px;
}

.lists .shop .products .item>div:not(.product-detail) {
    width: 125px;
}

.lists .shop .products .item .price p {
    margin: 3px 0;
}

.lists .shop .products .item .price p.market-price {
    color: #9c9c9c;
    text-decoration: line-through;
}

.lists .shop .products .item .price p.real-price {
    color: #000;
    font-weight: 700;
}

.lists .shop .products .item .count {
    font-size: 0;
}

.lists .shop .products .item .count span {
    display: inline-block;
    height: 23px;
    line-height: 20px;
    font-size: 14px;
    text-align: center;
}

.sub, .lists .shop .products .item .count span.add {
    background-color: #f0f0f0;
    color: #000;
    width: 17px;
    border: 1px solid #e5e5e5;
}

.lists .shop .products .item .count span.sub:hover, .lists .shop .products .item .count span.add:hover {
    color: #ffffff;
    background-color: #000000;
}
span .sub_s{
    color: #e1dfdf !important;
}
.lists .shop .products .item .count span.input {
    border: none;
}

.lists .shop .products .item .count span.input input {
    width: 40px;
    height: 21px;
    text-align: center;
    border: none;
}

.lists .shop .products .item .amount {
    color: #ff4400;
    font-weight: bold;
    font-size: 14px;
}

.lists .shop .products .item .delete a {
    color: #3c3c3c;
    text-decoration: none;
}

.lists .shop .products .item .delete a:hover {
    text-decoration: underline;
    color: #ff4400;
}

.operate {
    height: 50px;
    background-color: #93d2f3;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #3c3c3c;
    position: sticky;
    bottom: 0;
    left: 0;
}

.operate .left {
    padding-left: 10px;
}

.operate .left a {
    text-decoration: none;
    color: #3c3c3c;
    margin-left: 30px;
}

.operate .left a:hover {
    text-decoration: underline;
    color: #ff4400;
}

.operate .right {
    display: flex;
    justify-content: end;
    align-items: center;
}

.operate .right .amount {
    margin-left: 40px;
}

.operate .right .selected-count, .operate .right .sum {
    color: #ff4400;
    font-size: 18px;
    font-weight: 600;
    margin: 0 5px;
}

.operate .right a {
    height: 50px;
    line-height: 50px;
    width: 120px;
    background-color: #b0b0b0;
    text-align: center;
    text-decoration: none;
    color: #fff;
    font-size: 20px;
}

.operate .right a.active {
    background-color: #ff4400;
}

.operate .right a.active:hover {
    background-color: #f22d00;
}
</style>
